package com.esri.core.geometry;

import com.esri.core.geometry.Geometry;
import com.github.mikephil.charting.utils.Utils;
import java.io.Serializable;

/* loaded from: classes.dex */
public final class Line extends Segment implements Serializable {
    private static final long serialVersionUID = 2;

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // com.esri.core.geometry.Segment
    public int a(SegmentBuffer[] segmentBufferArr) {
        return 0;
    }

    @Override // com.esri.core.geometry.Segment
    void a(Segment segment) {
    }

    @Override // com.esri.core.geometry.Segment
    public boolean isCurve() {
        return false;
    }

    @Override // com.esri.core.geometry.Geometry
    public Geometry.Type getType() {
        return Geometry.Type.LINE;
    }

    @Override // com.esri.core.geometry.Geometry
    public double calculateLength2D() {
        double d = this.a - this.c;
        double d2 = this.b - this.d;
        return Math.sqrt((d * d) + (d2 * d2));
    }

    @Override // com.esri.core.geometry.Segment
    Point2D a(double d) {
        Point2D point2D = new Point2D();
        point2D.a(j(), a());
        return point2D;
    }

    @Override // com.esri.core.geometry.Segment
    boolean b(double d) {
        return calculateLength2D() <= d;
    }

    @Override // com.esri.core.geometry.Segment
    double c(double d) {
        Point2D d2 = d(d);
        d2.sub(a());
        return d2.length();
    }

    public Line() {
        this.m_description = VertexDescriptionDesignerImpl.d();
    }

    Line(VertexDescription vertexDescription) {
        this.m_description = vertexDescription;
    }

    Line(double d, double d2, double d3, double d4) {
        this.m_description = VertexDescriptionDesignerImpl.d();
        e(d, d2);
        f(d3, d4);
    }

    @Override // com.esri.core.geometry.Geometry
    public void queryEnvelope(Envelope envelope) {
        envelope.setEmpty();
        envelope.a(this.m_description);
        Envelope2D envelope2D = new Envelope2D();
        queryEnvelope2D(envelope2D);
        envelope.a(envelope2D);
        int attributeCount = this.m_description.getAttributeCount();
        int i = 1;
        while (i < attributeCount) {
            int semantics = this.m_description.getSemantics(i);
            int componentCount = VertexDescription.getComponentCount(semantics);
            while (i < componentCount) {
                envelope.a(semantics, 0, queryInterval(semantics, 0));
                i++;
            }
            i++;
        }
    }

    @Override // com.esri.core.geometry.Geometry
    public void queryEnvelope2D(Envelope2D envelope2D) {
        envelope2D.setCoords(this.a, this.b, this.c, this.d);
        envelope2D.normalize();
    }

    @Override // com.esri.core.geometry.Geometry
    void queryEnvelope3D(Envelope3D envelope3D) {
        envelope3D.setEmpty();
        envelope3D.merge(this.a, this.b, c(0, 1, 0));
        envelope3D.merge(this.c, this.d, c(1, 1, 0));
    }

    @Override // com.esri.core.geometry.Geometry
    public void applyTransformation(Transformation2D transformation2D) {
        i();
        Point2D point2D = new Point2D();
        point2D.x = this.a;
        point2D.y = this.b;
        transformation2D.a(point2D, point2D);
        this.a = point2D.x;
        this.b = point2D.y;
        point2D.x = this.c;
        point2D.y = this.d;
        transformation2D.a(point2D, point2D);
        this.c = point2D.x;
        this.d = point2D.y;
    }

    @Override // com.esri.core.geometry.Geometry
    void applyTransformation(Transformation3D transformation3D) {
        i();
        Point3D point3D = new Point3D();
        point3D.x = this.a;
        point3D.y = this.b;
        point3D.z = c(0, 1, 0);
        Point3D transform = transformation3D.transform(point3D);
        this.a = transform.x;
        this.b = transform.y;
        a(0, 1, 0, transform.z);
        transform.x = this.c;
        transform.y = this.d;
        transform.z = c(1, 1, 0);
        Point3D transform2 = transformation3D.transform(transform);
        this.c = transform2.x;
        this.d = transform2.y;
        a(1, 1, 0, transform2.z);
    }

    @Override // com.esri.core.geometry.Geometry
    public Geometry createInstance() {
        return new Line(this.m_description);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // com.esri.core.geometry.Segment
    public double a(double d, double d2) {
        return ((this.c - d) - (this.a - d)) * ((this.d - d2) + (this.b - d2)) * 0.5d;
    }

    @Override // com.esri.core.geometry.Segment
    void a(double d, Point2D point2D) {
        double d2 = 1.0d - d;
        point2D.setCoords((this.c * d) + (this.a * d2), (this.d * d) + (this.b * d2));
    }

    @Override // com.esri.core.geometry.Segment
    Segment b(double d, double d2) {
        SegmentBuffer segmentBuffer = new SegmentBuffer();
        a(d, d2, segmentBuffer);
        return segmentBuffer.get();
    }

    @Override // com.esri.core.geometry.Segment
    void a(double d, double d2, SegmentBuffer segmentBuffer) {
        if (segmentBuffer == null) {
            throw new IllegalArgumentException();
        }
        segmentBuffer.createLine();
        Segment segment = segmentBuffer.get();
        segment.a(this.m_description);
        Point2D point2D = new Point2D();
        a(d, point2D);
        segment.e(point2D.x, point2D.y);
        a(d2, point2D);
        segment.f(point2D.x, point2D.y);
        int attributeCount = this.m_description.getAttributeCount();
        for (int i = 1; i < attributeCount; i++) {
            int _getSemanticsImpl = this.m_description._getSemanticsImpl(i);
            int componentCount = VertexDescription.getComponentCount(_getSemanticsImpl);
            for (int i2 = 0; i2 < componentCount; i2++) {
                segment.a(_getSemanticsImpl, i2, a(d, _getSemanticsImpl, i2));
                segment.b(_getSemanticsImpl, i2, a(d2, _getSemanticsImpl, i2));
            }
        }
    }

    @Override // com.esri.core.geometry.Segment
    double a(double d, int i, int i2) {
        if (i == 0) {
            Point2D d2 = d(d);
            return i2 == 0 ? d2.x : d2.y;
        }
        int interpolation = VertexDescription.getInterpolation(i);
        if (interpolation == 0) {
            if (d < 0.5d) {
                return a(i, i2);
            }
            return c(i, i2);
        } else if (interpolation == 1) {
            return (a(i, i2) * (1.0d - d)) + (c(i, i2) * d);
        } else if (interpolation == 2) {
            throw new GeometryException("not implemented");
        } else {
            throw new GeometryException("internal error");
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // com.esri.core.geometry.Segment
    public double a(Point2D point2D, boolean z) {
        double d = this.c - this.a;
        double d2 = this.d - this.b;
        double d3 = (d * d) + (d2 * d2);
        if (d3 == Utils.DOUBLE_EPSILON) {
            return 0.5d;
        }
        double d4 = (((point2D.x - this.a) * d) + ((point2D.y - this.b) * d2)) / d3;
        if (!z) {
            if (d4 < Utils.DOUBLE_EPSILON) {
                return Utils.DOUBLE_EPSILON;
            }
            if (d4 > 1.0d) {
                return 1.0d;
            }
        }
        return d4;
    }

    @Override // com.esri.core.geometry.Segment
    public int intersectionWithAxis2D(boolean z, double d, double[] dArr, double[] dArr2) {
        if (z) {
            double d2 = this.d - this.b;
            if (d2 == Utils.DOUBLE_EPSILON) {
                return d == this.d ? -1 : 0;
            }
            double d3 = (d - this.b) / d2;
            if (d3 < Utils.DOUBLE_EPSILON || d3 > 1.0d) {
                return 0;
            }
            if (dArr != null) {
                dArr[0] = ((this.c - this.a) * d3) + this.a;
            }
            if (dArr2 != null) {
                dArr2[0] = d3;
            }
            return 1;
        }
        double d4 = this.c - this.a;
        if (d4 == Utils.DOUBLE_EPSILON) {
            return d == this.c ? -1 : 0;
        }
        double d5 = (d - this.a) / d4;
        if (d5 < Utils.DOUBLE_EPSILON || d5 > 1.0d) {
            return 0;
        }
        if (dArr != null) {
            dArr[0] = ((this.d - this.b) * d5) + this.a;
        }
        if (dArr2 != null) {
            dArr2[0] = d5;
        }
        return 1;
    }

    int a(Envelope2D envelope2D, boolean z, double[] dArr, double[] dArr2) {
        return envelope2D.a(a(), j(), 0, dArr, dArr2) != 0 ? 2 : 0;
    }

    @Override // com.esri.core.geometry.Segment
    double c(double d, double d2) {
        double d3 = this.d - this.b;
        if (d3 == Utils.DOUBLE_EPSILON) {
            return d == this.d ? d2 : NumberUtils.NaN();
        }
        double d4 = (d - this.b) / d3;
        return (this.c * d4) + ((1.0d - d4) * this.a);
    }

    @Override // com.esri.core.geometry.Segment
    boolean a(Point2D point2D, double d, boolean z) {
        return b(point2D, d, z) >= Utils.DOUBLE_EPSILON;
    }

    int a(Point2D point2D) {
        return d(point2D.x, point2D.y);
    }

    int d(double d, double d2) {
        Point2D point2D = new Point2D(d, d2);
        point2D.sub(a());
        Point2D point2D2 = new Point2D();
        point2D2.a(j(), a());
        double e = point2D2.e(point2D);
        double doubleEps = NumberUtils.doubleEps() * 4.0d * (Math.abs(point2D2.x * point2D.y) + Math.abs(point2D2.y * point2D.x));
        if (e > doubleEps) {
            return -1;
        }
        return e < (-doubleEps) ? 1 : 0;
    }

    double b(Point2D point2D, double d, boolean z) {
        Point2D point2D2 = new Point2D();
        Point2D point2D3 = new Point2D(this.a, this.b);
        point2D2.a(point2D, point2D3);
        double length = point2D2.length();
        if (length <= Math.max(d, length * 3.0d * NumberUtils.doubleEps())) {
            return (z && length == Utils.DOUBLE_EPSILON) ? NumberUtils.NaN() : Utils.DOUBLE_EPSILON;
        }
        point2D2.a(point2D, j());
        double length2 = point2D2.length();
        if (length2 <= Math.max(d, 3.0d * length2 * NumberUtils.doubleEps())) {
            if (z && length2 == Utils.DOUBLE_EPSILON) {
                return NumberUtils.NaN();
            }
            return 1.0d;
        }
        point2D2.setCoords(this.c - this.a, this.d - this.b);
        double length3 = point2D2.length();
        if (length3 > Utils.DOUBLE_EPSILON) {
            double d2 = 1.0d / length3;
            point2D2.scale(d2);
            Point2D point2D4 = new Point2D();
            point2D4.a(point2D, point2D3);
            double c = point2D4.c(point2D2);
            double d3 = point2D4.d(point2D2) * 8.0d * NumberUtils.doubleEps();
            point2D2.b();
            double c2 = point2D4.c(point2D2);
            double d4 = point2D4.d(point2D2) * 8.0d * NumberUtils.doubleEps();
            double max = Math.max(d, d3);
            if (c < (-max) || c > length3 + max) {
                return NumberUtils.NaN();
            }
            if (Math.abs(c2) <= Math.max(d, d4)) {
                double d5 = c * d2;
                if (d5 < Utils.DOUBLE_EPSILON) {
                    return Utils.DOUBLE_EPSILON;
                }
                if (d5 > 1.0d) {
                    return 1.0d;
                }
                return d5;
            }
        }
        return NumberUtils.NaN();
    }

    boolean a(Line line) {
        if (line == this) {
            return true;
        }
        if (line instanceof Line) {
            return false;
        }
        return b((Segment) line);
    }

    boolean a(Line line, Point2D point2D, boolean z) {
        double d = z ? this.a : this.c;
        double d2 = z ? this.b : this.d;
        Point2D point2D2 = new Point2D();
        point2D2.x = line.getEndX() - d;
        point2D2.y = line.getEndY() - d2;
        if (point2D.c(point2D2) > NumberUtils.doubleEps() * 3.0d * point2D.d(point2D2)) {
            point2D2.x = line.getStartX() - d;
            point2D2.y = line.getStartY() - d2;
            return point2D.c(point2D2) <= (NumberUtils.doubleEps() * 3.0d) * point2D.d(point2D2);
        }
        return true;
    }

    boolean b(Line line) {
        Point2D point2D = new Point2D();
        point2D.x = this.c - this.a;
        point2D.y = this.d - this.b;
        if (a(line, point2D, false)) {
            point2D.negate();
            return a(line, point2D, true);
        }
        return false;
    }

    static boolean a(Line line, Line line2) {
        int d = line.d(line2.a, line2.b);
        int d2 = line.d(line2.c, line2.d);
        if ((d >= 0 || d2 >= 0) && (d <= 0 || d2 <= 0)) {
            int d3 = line2.d(line.a, line.b);
            int d4 = line2.d(line.c, line.d);
            if ((d3 >= 0 || d4 >= 0) && (d3 <= 0 || d4 <= 0)) {
                if (line.calculateLength2D() > line2.calculateLength2D()) {
                    return line.b(line2);
                }
                return line2.b(line);
            }
            return false;
        }
        return false;
    }

    static Point2D a(Line line, Line line2, double d) {
        Point2D point2D = new Point2D(NumberUtils.NaN(), NumberUtils.NaN());
        double d2 = line.c - line.a;
        double d3 = line.d - line.b;
        double d4 = line2.c - line2.a;
        double d5 = line2.d - line2.b;
        double d6 = d4 * d3;
        double d7 = d2 * d5;
        double d8 = d6 - d7;
        if (d8 == Utils.DOUBLE_EPSILON) {
            return point2D;
        }
        double doubleEps = NumberUtils.doubleEps() * 4.0d * (Math.abs(d6) + Math.abs(d7));
        double d9 = line2.a - line.a;
        double d10 = line2.b - line.b;
        double d11 = d4 * d10;
        double d12 = d5 * d9;
        double d13 = d11 - d12;
        double doubleEps2 = NumberUtils.doubleEps() * 4.0d * (Math.abs(d11) + Math.abs(d12));
        double d14 = d13 / d8;
        double abs = Math.abs(d8);
        double abs2 = (doubleEps2 * abs) + (Math.abs(d13) * doubleEps);
        double d15 = d8 * d8;
        double doubleEps3 = (abs2 / d15) + (NumberUtils.doubleEps() * Math.abs(d14));
        if (d14 < (-doubleEps3) || d14 > doubleEps3 + 1.0d) {
            return point2D;
        }
        double d16 = d2 * d10;
        double d17 = d9 * d3;
        double d18 = d16 - d17;
        double d19 = d18 / d8;
        double doubleEps4 = (((((NumberUtils.doubleEps() * 4.0d) * (Math.abs(d16) + Math.abs(d17))) * abs) + (doubleEps * Math.abs(d18))) / d15) + (NumberUtils.doubleEps() * Math.abs(d19));
        if (d19 < (-doubleEps4) || d19 > doubleEps4 + 1.0d) {
            return point2D;
        }
        double snap = NumberUtils.snap(d14, Utils.DOUBLE_EPSILON, 1.0d);
        double snap2 = NumberUtils.snap(d19, Utils.DOUBLE_EPSILON, 1.0d);
        Point2D d20 = line.d(snap);
        Point2D d21 = line2.d(snap2);
        Point2D point2D2 = new Point2D();
        point2D2.a(d20, d21);
        if (point2D2.length() > d) {
            point2D2.add(d20, d21);
            point2D2.scale(0.5d);
            double a = line.a(point2D2, false);
            double a2 = line2.a(point2D2, false);
            Point2D d22 = line.d(a);
            d22.sub(line2.d(a2));
            if (d22.length() > d) {
                return point2D;
            }
            snap = a;
            snap2 = a2;
        }
        point2D.setCoords(snap, snap2);
        return point2D;
    }

    public static int a(Line line, Line line2, double d, boolean z) {
        int i;
        if ((line.a != line2.a || line.b != line2.b) && (line.a != line2.c || line.b != line2.d)) {
            i = 0;
        } else if (!z) {
            return 1;
        } else {
            i = 1;
        }
        if ((line.c == line2.a && line.d == line2.b) || (line.c == line2.c && line.d == line2.d)) {
            i++;
            if (i == 2) {
                return 2;
            }
            if (!z) {
                return 1;
            }
        }
        if (line2.a(line.a(), d, true) || line2.a(line.j(), d, true) || line.a(line2.a(), d, true) || line.a(line2.j(), d, true)) {
            return 1;
        }
        if (!z || i == 0) {
            return a(line, line2) ? 1 : 0;
        }
        return 0;
    }

    int a(Line line, Line line2, Point2D[] point2DArr, double[] dArr, double[] dArr2) {
        int i;
        if (line.a == line2.a && line.b == line2.b) {
            if (dArr != null) {
                dArr[0] = 0.0d;
            }
            if (dArr2 != null) {
                dArr2[0] = 0.0d;
            }
            if (point2DArr != null) {
                point2DArr[0] = Point2D.construct(line.a, line.b);
            }
            i = 1;
        } else {
            i = 0;
        }
        if (line.a == line2.c && line.b == line2.d) {
            if (dArr != null) {
                dArr[i] = 0.0d;
            }
            if (dArr2 != null) {
                dArr2[i] = 1.0d;
            }
            if (point2DArr != null) {
                point2DArr[i] = Point2D.construct(line.a, line.b);
            }
            i++;
        }
        if (line.c == line2.a && line.d == line2.b) {
            if (i == 2) {
                if (dArr != null) {
                    dArr[0] = 0.0d;
                    dArr[1] = 1.0d;
                }
                if (dArr2 != null) {
                    dArr2[0] = 1.0d;
                }
                if (point2DArr != null) {
                    point2DArr[0] = Point2D.construct(line.c, line.d);
                    point2DArr[1] = Point2D.construct(line.c, line.d);
                }
                return i;
            }
            if (dArr != null) {
                dArr[i] = 1.0d;
            }
            if (dArr2 != null) {
                dArr2[i] = 0.0d;
            }
            if (point2DArr != null) {
                point2DArr[i] = Point2D.construct(line.c, line.d);
            }
            i++;
        }
        if (line.c == line2.c && line.d == line2.d) {
            if (i != 2) {
                if (dArr != null) {
                    dArr[i] = 1.0d;
                }
                if (dArr2 != null) {
                    dArr2[i] = 1.0d;
                }
                if (point2DArr != null) {
                    point2DArr[i] = Point2D.construct(line.c, line.d);
                }
                return i + 1;
            }
            if (dArr != null) {
                dArr[0] = 0.0d;
                dArr[1] = 1.0d;
            }
            if (dArr2 != null) {
                dArr2[0] = 1.0d;
            }
            if (point2DArr != null) {
                point2DArr[0] = Point2D.construct(line.c, line.d);
                point2DArr[1] = Point2D.construct(line.c, line.d);
            }
            return i;
        }
        return i;
    }

    public static int a(Line line, Line line2, Point2D[] point2DArr, double[] dArr, double[] dArr2, double d) {
        int i;
        double b = line.b(line2.a(), d, false);
        double b2 = line.b(line2.j(), d, false);
        double b3 = line2.b(line.a(), d, false);
        double b4 = line2.b(line.j(), d, false);
        if (NumberUtils.isNaN(b)) {
            i = 0;
        } else {
            if (dArr != null) {
                dArr[0] = b;
            }
            if (dArr2 != null) {
                dArr2[0] = 0.0d;
            }
            if (point2DArr != null) {
                point2DArr[0] = Point2D.construct(line2.a, line2.b);
            }
            i = 1;
        }
        if (!NumberUtils.isNaN(b2)) {
            if (dArr != null) {
                dArr[i] = b2;
            }
            if (dArr2 != null) {
                dArr2[i] = 1.0d;
            }
            if (point2DArr != null) {
                point2DArr[i] = Point2D.construct(line2.c, line2.d);
            }
            i++;
        }
        if (i != 2 && !NumberUtils.isNaN(b3) && ((b != Utils.DOUBLE_EPSILON || b3 != Utils.DOUBLE_EPSILON) && (b2 != Utils.DOUBLE_EPSILON || b3 != 1.0d))) {
            if (dArr != null) {
                dArr[i] = 0.0d;
            }
            if (dArr2 != null) {
                dArr2[i] = b3;
            }
            if (point2DArr != null) {
                point2DArr[i] = Point2D.construct(line.a, line.b);
            }
            i++;
        }
        if (i != 2 && !NumberUtils.isNaN(b4) && ((b != 1.0d || b4 != Utils.DOUBLE_EPSILON) && (b2 != 1.0d || b4 != 1.0d))) {
            if (dArr != null) {
                dArr[i] = 1.0d;
            }
            if (dArr2 != null) {
                dArr2[i] = b4;
            }
            if (point2DArr != null) {
                point2DArr[i] = Point2D.construct(line2.c, line2.d);
            }
            i++;
        }
        if (i > 0) {
            if (i == 2 && dArr != null && dArr[0] > dArr[1]) {
                double d2 = dArr[0];
                dArr[0] = dArr[1];
                dArr[1] = d2;
                if (dArr2 != null) {
                    double d3 = dArr2[0];
                    dArr2[0] = dArr2[1];
                    dArr2[1] = d3;
                }
                if (point2DArr != null) {
                    Point2D point2D = new Point2D(point2DArr[0].x, point2DArr[0].y);
                    point2DArr[0] = point2DArr[1];
                    point2DArr[1] = point2D;
                }
            }
            return i;
        }
        Point2D a = a(line, line2, d);
        if (NumberUtils.isNaN(a.x)) {
            return 0;
        }
        if (point2DArr != null) {
            point2DArr[0] = line.d(a.x);
        }
        if (dArr != null) {
            dArr[0] = a.x;
        }
        if (dArr2 != null) {
            dArr2[0] = a.y;
        }
        return 1;
    }

    @Override // com.esri.core.geometry.Geometry
    public boolean isValid() {
        return (NumberUtils.isNaN(getStartX()) || NumberUtils.isNaN(getStartY()) || NumberUtils.isNaN(getEndX()) || NumberUtils.isNaN(getEndY())) ? false : true;
    }
}
